﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using Ninject.Web.Common;
using System.Net.Configuration;
using Realtors.Repository;
using Realtors.Repository.Fake;

namespace Realtors.Web.UI.Infrastructure
{
    public class NinjectControllerFactory:DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            RegisterServices();
        }

        private void RegisterServices()
        {
#if DEBUG
            ninjectKernel.Bind<IMailService>().To<FakeEmailService>().InRequestScope();
            ninjectKernel.Bind<IHouseRepository>().To<FakeHouseRepository>().InRequestScope();
#else
            var smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
            ninjectKernel.Bind<IMailService>().To<MailService>().
                WithConstructorArgument("emailTo", smtpSection.From);
#endif
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return (controllerType==null)?null : (IController) ninjectKernel.Get(controllerType);
        }
    }
}